package com.stone.inventory.sharding.id;

import com.stone.inventory.sharding.config.properties.GlobalIdProperties;
import lombok.Data;

/**
 * 表Id生成器
 *
 * @author Mr_wenpan@163.com 2021-07-27
 */
@Data
public class TableIdGenerator {

    /**
     * 服务实例id
     */
    private Long instanceId;

    /**
     * 分表id
     */
    private Long tableIndex;

    /**
     * 实际表名称
     */
    private String actualTableName;

    /**
     * id生成器
     */
    private IdGenerator idGenerator;

    /**
     * 全局的id生成属性配置
     */
    private GlobalIdProperties globalIdProperties;

    public TableIdGenerator(Long tableIndex, String actualTableName, GlobalIdProperties globalIdProperties) {
        instanceId = globalIdProperties.getInstanceId();
        this.tableIndex = tableIndex;
        this.actualTableName = actualTableName;
        this.globalIdProperties = globalIdProperties;
        idGenerator = new IdGenerator(globalIdProperties, instanceId, tableIndex);
    }

    /**
     * 获取下一个id
     *
     * @return id
     */
    public long nextId() {
        // 通过ID生成器生成一个全局唯一ID
        return idGenerator.nextId();
    }
}
